package com.example.ytyproject.controller;

import com.example.ytyproject.entity.vo.R;
import com.example.ytyproject.utils.QRCodeUtil;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * 二维码
 */

@RestController
@RequestMapping("/qrCode")
public class QrCodeController {

    @GetMapping("/getQrcode")
    @ResponseBody
    public R getQrcode(@RequestParam("userId") String userId, HttpServletRequest request) throws Exception {
        // 调换的连接
        String redicectUrl = "http://host.docker.internal:8848/#/path";  //这里设置自定义网站url
        // 二维码存储路径
        String pathName = ResourceUtils.getURL("classpath:static").getPath();

        String filePath = "/qrCodeImage"; // 文件存储路径
        File path = new File(pathName, filePath);
        if (!path.exists()) {
            path.mkdirs();
        }
        String fileName = QRCodeUtil.encode(redicectUrl, path.getPath(), true, userId);
        StringBuffer requestURL = request.getRequestURL();
        String frex = "/api";
        String host = requestURL.substring(0, requestURL.indexOf(frex)) + frex;
        return R.ok(host + filePath + "/" + fileName);
    }
}
